import QtQuick
import QtQuick3D
import QtQuickRO

import "../EventBus"
/*
网络框架，包含一局游戏中网络方面相关的设置
*/

Item{
    id:level

    property Component sourceComp//场景组件，同时也是客户端服务端同步对象树的根

    property Item item:{//场景组件生成的实体
        if(mode === Network.None)
        {
            return null
        }
        if(mode === Network.Client)
        {
            return node.rootObject
        }

        return host.rootObject
    }

    function startAsServer(port){
        hostPort = port
        mode = Network.Server
    }

    function startAsListenServer(port){
        hostPort = port
        mode = Network.ListenServer
    }

    function startOffline(){
        mode = Network.Offline
    }

    function startAsClient(host){
        connectAddress = host
        mode = Network.Client
    }

    Item {
        id: __materialLibrary__
    }

    //该关卡的启动方式
    enum Mode{
        None,//无效变量
        Server,//纯服务端
        ListenServer,//监听服务端
        Offline,//离线模式
        Client//客户端
    }

    property int mode:Network.None//默认不启动

    onModeChanged: {
        if(mode === Network.Server || mode === Network.ListenServer || mode === Network.Offline)
        {
            if(mode === Network.Server)
            {
                host.policy = ROHost.Server
            }
            else if(mode === Network.ListenServer)
            {
                host.policy = ROHost.ListenServer
            }
            else if(mode === Network.Offline)
            {
                host.policy = ROHost.Offline
            }

            host.active = true
        }
        else if(mode === Network.Client)
        {
            node.active = true
        }
    }

    property alias host:host
    property alias node:node

    property alias hostPort:host.port//当以主机模式启动时，使用该参数控制开放端口

    property alias connectAddress:node.host//当使用客户端模式启动时，使用该参数连接到主机。示例:"qtro://ip:port"

    property var loginParam:({}) //登录参数，当客户端连接到服务端，或服务端以ListenServer，Offline模式启动时。该参数会附加到对应的player上

    property Component playerComp//玩家对象的组件

    //当前主机对应的玩家id，纯服务端为0
    property int currentPlayerId:{
        if(mode === Network.None || mode === Network.Server)
        {
            return 0
        }
        else if(mode === Network.ListenServer || mode === Network.Offline)
        {
            for(var i in host.players)
            {
                let player = host.players[i]
                if(player.isHostPlayer)
                {
                    return player.playerId
                }
            }
        }
        else if(mode === Network.Client)
        {
            return node.playerId
        }

        return 0
    }

    ROHost{
        id:host
        active: false
        rootComponent: sourceComp
        port:2425

        function getPlayerById(playerId){
            for(var i in host.players)
            {
                let player = host.players[i]
                if(player.playerId === playerId)
                {
                    return player
                }
            }

            return null
        }

        hostPlayerParam: level.loginParam
        playerDelegate: playerComp
    }

    RONode{
        id:node

        active:mode === EventBus.Client

        rootComponent: sourceComp
        onErrStringChanged: {
            console.log(errString)
        }

        loginParam:level.loginParam
    }

}
